[% setvar title More direct syntax for hashes %]
<div id="archive-notice">
    <h3>This file is part of the Perl 6 Archive</h3>
    <p>To see what is currently happening visit <a href="http://www.perl6.org/">http://www.perl6.org/</a></p>
</div>
<div class='pod'>
<a name='TITLE'></a><h1>TITLE</h1>
<p>More direct syntax for hashes</p>
<a name='VERSION'></a><h1>VERSION</h1>
<pre>  Maintainer: Nathan Torkington &lt;<a href='mailto:gnat@frii.com'>gnat@frii.com</a>&gt;
  Date: 5 Sep 2000
  Last Modified: 15 Sep 2000
  Mailing List: <a href='mailto:perl6-language-data@perl.org'>perl6-language-data@perl.org</a>
  Number: 196
  Version: 2
  Status: Frozen</pre>
<a name='ABSTRACT'></a><h1>ABSTRACT</h1>
<p><code>scalar(%hash)</code> should return what <code>scalar(keys %hash)</code> currently
returns.</p>
<p><code>reset %hash</code> should reset the hash iterator, instead of calling
<code>keys</code> or <code>values</code> as is currently the case.</p>
<p>The parser should special-case the variations of <code>sort %hash</code> so
that it returns the keys and value, calling the comparison function
for keys.</p>
<a name='CHANGES'></a><h1>CHANGES</h1>
<pre> * Clarified that new reset() syntax doesn't clash with existing
 * Fixed braino in description of &quot;sort %hash&quot;</pre>
<a name='DESCRIPTION'></a><h1>DESCRIPTION</h1>
<p>While Perl has hashes as a built-in data type, the mechanism for
working with hashes is often built on top of list primitives.  While
this is acceptable, it's not as convenient as it could be.  I'm
arguing for more direct support of hashes in the language.</p>
<p>Proposal 1 is that a hash in scalar context evaluate to the number
of keys in the hash.  You can find that out now, but only by using
the <code>keys()</code> function in scalar context.</p>
<p>Currently <code>%hash</code> in scalar context returns a false value if %hash
is empty, or a string like &quot;4/8&quot; showing how full the hash data
structure is.  This string is rarely useful to the programmer.
Mostly it's just used for its true/false value:</p>
<pre>  if (%hash) { ... }</pre>
<p>Proposal 1 would retain that use, but also make:</p>
<pre>  $count = %hash;</pre>
<p>analogous to</p>
<pre>  $count = @array;</pre>
<p>Proposal 2 is that the iterator in a hash be reset through an explicit
call to the <code>reset()</code> function.  In perl5, one must call <code>keys()</code>
or <code>values()</code> to reset the iterator, an odd overloading of these
functions behaviour.  I propose that the <code>keys()</code> and <code>values()</code>
functions no longer have this side-effect, but instead reset() be
used:</p>
<pre>  keys %hash;	# reset the iterator in perl5
  reset %hash;	# same but in in perl6</pre>
<p>This function more obviously describes what is happening.  reset()
also has a meaning, but that functionality does not clash
syntactically with this new meaning.  In any event, the move away
from global symbol table actions will probably remove the current
functionality of reset().</p>
<p>Proposal 3 is to have the parser identify <code>sort %hash</code> and its
variations, and automatically rewrite it.
I'd like to be able to say:</p>
<pre>  foreach ($k,$v) (sort %hash) { ... }</pre>
<p>This would be equivalent to:</p>
<pre>  foreach ($k,$v) (map { $_ =&gt; $hash{$_}
		   sort
		   keys %hash)
  { ... }</pre>
<p>Similarly one should be able to use a sort comparison function with
a hash.  I do not expect this hash knowledge to apply to function
calls or anything else that might return key-value pairs.  This is
purely when the data to be sorted is in a hash variable.</p>
<p>This relies on RFC 173's foreach() extensions to be useful.</p>
<a name='IMPLEMENTATION'></a><h1>IMPLEMENTATION</h1>
<p>Proposal 1 simply changes the scalar value of a hash.  The old
functionality would have to be available from a module for the perl526
program to be able to translate any program that relied on this
knowledge of the data structure (there are a few, though not many,
that do).</p>
<p>Proposal 2 removes the side-effects from keys() and values(), and
puts it into reset().  The reset() function is going to need a
profound overhaul anyway (given how intensely symbol-table driven
it is) and it is the obvious place for this functionality.</p>
<p>Proposal 3 could be done in the parser as a rewrite of the source
code.  However, I suspect it would run faster if a flag on the sort()
op said &quot;you're getting a hash structure&quot; and sort() took care of
it all internally.  That'd avoid multiple op dispatches.  This is
an implementation decision for better performance, though.  At the
bare minimum, source code rewriting would implement the function
with acceptable performance.</p>
<a name='REFERENCES'></a><h1>REFERENCES</h1>
<p>RFC 173: Allow multiple loop variables in foreach statements</p>
<p>perlfunc manpage for keys(), values() and reset() documentation</p>
<p>perlsyn manpage for foreach() documentation</p>
</div>
